from dataclasses import field
from distutils.command.config import config
from pydoc import describe
from statistics import mode
from tortoise import models
from tortoise import fields
from tortoise.exceptions import NoValuesFetched
from core import settings
from . import baseModel
from . import User


class ProjectBatches(models.Model,baseModel.TimeBaseMode):
    batch_name = fields.CharField(max_length=50,description="项目批次",unique=True)
    group_name = fields.CharEnumField(settings.ProjectGroupName,null=False,description="批次所属项目组")
    project_batch : fields.ReverseRelation["Project"]
    # project_batch2 : fields.ReverseRelation["Project"]
    
    class Meta:
        table = "project_batches"
        table_description = "项目批次表"
        
    class PydanticMeta:
        exclude = ("project_batch")
        
class Project(models.Model,baseModel.TimeBaseMode):
    project_owner = fields.ForeignKeyField(
        "models.User",related_name="projects",to_field="username"
    )
    project_code = fields.CharField(max_length=100,description="项目编码",unique=True)
    project_name = fields.CharField(max_length=200,description="项目名",unique=True)
    department = fields.CharEnumField(settings.Department,description="部门")
    project_class = fields.CharEnumField(settings.ProjectClass,description="项目类型（个人/团队）")
    project_type = fields.CharEnumField(settings.ProjectType,description="项目规模类型")
    project_background = fields.CharField(max_length=300,description="项目背景")
    project_brief = fields.CharField(max_length=500,description="项目简介")
    project_annotation = fields.CharField(max_length=300,description="项目创新点")
    project_effect = fields.CharField(max_length=300,description="项目预期效果/推广价值")
    statu = fields.CharEnumField(settings.ProjectStatus,null=False,description="项目状态")
    project_group = fields.CharEnumField(settings.ProjectGroupName,description="组名称")
    
    project_batch : fields.ForeignKeyNullableRelation[ProjectBatches] = fields.ForeignKeyField(
        "models.ProjectBatches",related_name="project_batch",to_field="batch_name",null=True,description="项目所属专家组批次"
    )
    
    # project_batch2 : fields.ForeignKeyNullableRelation[ProjectBatches] = fields.ForeignKeyField(
    #     "models.ProjectBatches",related_name="project_batch2",to_field="batch_name",null=True,description="项目所属创委会批次"
    # )
    
    project_member : fields.ReverseRelation["ProjectMembers"]
    audit_records : fields.ReverseRelation["ProjectAuditRecords"]
    project_file : fields.ReverseRelation["ProjectFiles"]
    project_log : fields.ReverseRelation["ProjectLog"]
    
    # def audit_records_num(self) -> int:
    #     try:
    #         return len(self.audit_records)
    #     except NoValuesFetched:
    #         return 0
    
    # class PydanticMeta:
    #     # Let's include two callables as computed columns
    #     computed = ("audit_records_num")
    
    class Meta:
        table = "project" # 指定表名
        table_description = "项目表"
        
        ordering = ["-id"]
    
class ProjectLog(models.Model,baseModel.TimeBaseMode):
    project : fields.ForeignKeyRelation["Project"] = fields.ForeignKeyField(
        "models.Project",related_name="project_log"
    )
    
    pre_statu = fields.CharField(max_length=50,null=True,description="修改前状态")
    statu = fields.CharField(max_length=50,description="修改后状态")
    
    user = fields.ForeignKeyField(
        "models.User",related_name="project_log",to_field="username"
    )
    project_group = fields.CharField(max_length=50,null=True,description="操作专家组名称")
    
    class Meta:
        table = "project_log"
        table_description = "项目日志表"

class ProjectFiles(models.Model,baseModel.TimeBaseMode):
    file_path = fields.CharField(max_length=50,null=False,description="文件路径")
    project : fields.ForeignKeyRelation["Project"] = fields.ForeignKeyField(
        "models.Project",related_name="project_file"
    )
    
    class Meta:
        table = "project_files"
        table_description = "项目文件路径表"

class ProjectGroups(models.Model,baseModel.TimeBaseMode):
    group_name = fields.CharEnumField(settings.ProjectGroupName,description="组名称")
    group_member_role = fields.CharEnumField(settings.ProjectGroupMemberRole,description="成员角色")
    group_member : fields.ForeignKeyRelation["User"] = fields.ForeignKeyField(
        "models.User",related_name="group_member",to_field="username",description="组成员"
    )
    
    # group_rule : fields.ForeignKeyNullableRelation["ProjectGroupsRules"]
    # audit_records : fields.ForeignKeyNullableRelation["ProjectAuditRecords"]
    
    class Meta:
        table = "project_groups"
        table_description = "项目评审组成员表"
        
class ProjectMembers(models.Model,baseModel.TimeBaseMode):
    project : fields.ForeignKeyRelation["Project"] = fields.ForeignKeyField(
        "models.Project",related_name="project_member"
    )
    # user : fields.ForeignKeyRelation["User"] = fields.ForeignKeyField(
    #     "models.User",related_name="project_member",to_field="username"
    # )
    user_fullname = fields.CharField(max_length=20,description="成员姓名")
    user_department = fields.CharEnumField(settings.Department,descrition="成员所在部门")
    members_job = fields.CharField(max_length=100,null=False,descrition="成员工作内容")
    menbers_percentage = fields.FloatField(description="成员贡献占比")
    
    class Meta:
        table = "project_members"
        table_description = "项目成员表"
        
        ordering = ["-menbers_percentage"]
        
class ProjectAuditRules(models.Model,baseModel.TimeBaseMode):
    group_name = fields.CharEnumField(settings.ProjectGroupName)
    rule_name = fields.CharField(max_length=20,null=False,description="规则名称")
    dimension = fields.CharField(max_length=20,null=False,description="打分维度")
    description = fields.TextField(description="维度描述")
    refrence_point = fields.TextField(description="参考分值")
    max_point = fields.IntField(description="最大分值")
    
    # group_rule : fields.ManyToManyRelation['ProjectGroupsRules']
    
    class Meta:
        table = "project_audit_rules"
        table_description = "项目评审规则表"
    
# class ProjectGroupsRules(models.Model,baseModel.TimeBaseMode):
#     group_name = fields.CharEnumField(settings.ProjectGroupName,description="组名称")
#     rules : fields.ManyToManyRelation[ProjectAuditRules] = fields.ManyToManyField(
#         "models.ProjectAuditRules",related_name="group_rule",description="项目评审组所对应打分规则"
#     )
    
#     class Meta:
#         table = "project_groups_rules"
#         table_description = "项目评审组打分规则对应表"

class ProjectAuditRecords(models.Model,baseModel.TimeBaseMode):
    project : fields.ForeignKeyRelation[Project] = fields.ForeignKeyField(
        "models.Project",related_name="audit_records"
    )
    group_name = fields.CharEnumField(settings.ProjectGroupName,description="组名称")
    user : fields.ForeignKeyRelation["User"] = fields.ForeignKeyField(
        "models.User",related_name="audit_records",to_field="username"
    )
    dimension = fields.CharField(max_length=20,null=False,description="打分维度")
    point = fields.FloatField(null=False,description="得分")
    
    class Meta:
        table = "project_audit_records"
        table_description = "项目评审组打分记录表"
        
    